package com.alibaba.gaiax.demo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.LinearLayoutCompat
import com.alibaba.fastjson.JSON
import com.alibaba.gaiax.GXRegisterCenter
import com.alibaba.gaiax.GXTemplateEngine
import com.alibaba.gaiax.template.GXTemplate
import com.alibaba.gaiax.template.GXTemplateKey
import com.alibaba.gaiax.utils.GXScreenUtils
import org.json.JSONObject
import java.io.File

class ZDMTemplateActivity : AppCompatActivity() {
    private var path = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_remote_data_source_template)

        // Init
        GXTemplateEngine.instance.init(this)

        // Register remote data template source to GaiaXSDK
        val netTemplateSource = RemoteDataSourceTemplateSource.instance
        GXRegisterCenter.instance.registerExtensionTemplateSource(netTemplateSource, 20)
        val bizId = "remote"
        val templateId = intent.getStringExtra("templateId") ?: ""
        path = baseContext.filesDir.absolutePath + "/" + templateId
        val data = JSONObject()
        val jsonText = File("$path/gaixa-demo.json").readText()
        data.put(GXTemplateKey.GAIAX_LAYER, jsonText)
        data.put(GXTemplateKey.GAIAX_CSS, File("$path/index.css").readText())
        data.put(GXTemplateKey.GAIAX_DATABINDING, File("$path/index.databinding").readText())
        RemoteDataSourceTemplateSource.instance.addTemplate(bizId, templateId, data)
        toBindTemplate(bizId, templateId)
    }

    private fun toBindTemplate(bizId: String, templateId: String) {

        // 模板参数
        // 去加载远程模板业务下的gx-vertical-item模板
        val params = GXTemplateEngine.GXTemplateItem(this, bizId, templateId)

        // 模板绘制尺寸
        val size = GXTemplateEngine.GXMeasureSize(GXScreenUtils.getScreenWidthPx(this), null)

        // 模板数据
        val templateData = GXTemplateEngine.GXTemplateData(
            JSON.parseObject(File("$path/index.data").readText())
        )

        // 创建模板View
        val view = GXTemplateEngine.instance.createView(params, size)!!

        // 绑定数据
        GXTemplateEngine.instance.bindData(view, templateData)

        // 插入模板View
        findViewById<LinearLayoutCompat>(R.id.root).addView(view, 0)
    }
}

/**
 * 远程模板的数据源
 */
class RemoteDataSourceTemplateSource : GXRegisterCenter.GXIExtensionTemplateSource {

    companion object {
        val instance by lazy {
            RemoteDataSourceTemplateSource()
        }
    }

    private val templateCache: MutableList<GXTemplate> = mutableListOf()

    override fun getTemplate(gxTemplateItem: GXTemplateEngine.GXTemplateItem): GXTemplate? {
        return templateCache.firstOrNull { it.biz == gxTemplateItem.bizId && it.id == gxTemplateItem.templateId }
    }

    fun addTemplate(templateBiz: String, templateId: String, binaryData: JSONObject?) {
        if (binaryData != null) {
            val layer = binaryData.getString(GXTemplateKey.GAIAX_LAYER)
                ?: throw IllegalArgumentException("Layer mustn't empty, templateBiz = $templateBiz, templateId = $templateId")
            val css = binaryData.getString(GXTemplateKey.GAIAX_CSS) ?: ""
            val dataBind = binaryData.getString(GXTemplateKey.GAIAX_DATABINDING) ?: ""
            val js = ""
            val template = GXTemplate(templateId, templateBiz, -1, layer, css, dataBind, js)
            templateCache.add(template)
        }
    }
}